package com.ruoyi.cust.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.cust.vo.CityTagTreeVo;
import com.ruoyi.cust.vo.CityTagVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cust.mapper.CityTagMapper;
import com.ruoyi.cust.domain.CityTag;
import com.ruoyi.cust.service.ICityTagService;

/**
 * 标签Service业务层处理
 *
 * @author cjq
 * @date 2024-12-06
 */
@Service
public class CityTagServiceImpl implements ICityTagService {
    @Autowired
    private CityTagMapper cityTagMapper;

    /**
     * 查询标签
     *
     * @param tagId 标签主键
     * @return 标签
     */
    @Override
    public CityTag selectCityTagByTagId(Long tagId) {
        return cityTagMapper.selectCityTagByTagId(tagId);
    }

    /**
     * 查询标签列表
     *
     * @param cityTag 标签
     * @return 标签
     */
    @Override
    public List<CityTag> selectCityTagList(CityTag cityTag) {
        return cityTagMapper.selectCityTagList(cityTag);
    }

    /**
     * 新增标签
     *
     * @param cityTag 标签
     * @return 结果
     */
    @Override
    public int insertCityTag(CityTag cityTag) {
                cityTag.setCreateTime(DateUtils.getNowDate());
            return cityTagMapper.insertCityTag(cityTag);
    }

    /**
     * 修改标签
     *
     * @param cityTag 标签
     * @return 结果
     */
    @Override
    public int updateCityTag(CityTag cityTag) {
        return cityTagMapper.updateCityTag(cityTag);
    }

    /**
     * 批量删除标签
     *
     * @param tagIds 需要删除的标签主键
     * @return 结果
     */
    @Override
    public int deleteCityTagByTagIds(Long[] tagIds) {
        return cityTagMapper.deleteCityTagByTagIds(tagIds);
    }

    /**
     * 删除标签信息
     *
     * @param tagId 标签主键
     * @return 结果
     */
    @Override
    public int deleteCityTagByTagId(Long tagId) {
        return cityTagMapper.deleteCityTagByTagId(tagId);
    }

    @Override
    public List<CityTagTreeVo> tagTree(CityTag cityTag) {
        List<CityTag> list = cityTagMapper.selectCityTagList(cityTag);
        Map<String, List<CityTagVo>> groupedTags = list.stream()
                .map(tag -> {
                    CityTagVo cityTagVo = new CityTagVo();
                    BeanUtils.copyProperties(tag, cityTagVo);
                    return cityTagVo;
                })
                .collect(Collectors.groupingBy(CityTagVo::getStoreType));

        return groupedTags.entrySet().stream()
                .map(entry -> {
                    CityTagTreeVo cityTagTreeVo = new CityTagTreeVo();
                    cityTagTreeVo.setStoreType(entry.getKey());
                    cityTagTreeVo.setCityTagList(entry.getValue());
                    return cityTagTreeVo;
                })
                .collect(Collectors.toList());
    }
}
